package face.pay.task.terminal.service.impl;


import face.pay.dao.TerminalTargetDayCountDao;
import face.pay.entity.TerminalTargetDayCount;
import face.pay.task.terminal.service.ITerminalTargetDayCountRelationService;
import face.pay.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Classname TerminalTargetCountServiceImpl
 * @Description JJL
 * @Date 2019/10/23 13:07
 */
@Service
@Slf4j
public class TerminalTargetDayCountRelationServiceImpl implements ITerminalTargetDayCountRelationService {

    @Autowired
    private TerminalTargetDayCountDao targetCountDao;


    @Override
    public void targetDayCount() {

        log.info("设备达标率日统计任务开始执行");
        String maxTime = targetCountDao.queryMaxTime();
        log.info("检测上一次数据日志"+maxTime);

        log.info("上一次统计时间"+maxTime);

        Date nowDate =  new Date();
        // DateUtil.dateParse("2019-11-08","yyyy-MM-dd");

        log.info("当前时间"+ DateUtil.getNowTime());


        Date dataDate = DateUtil.addDay(nowDate,-1);

        String dataDay = DateUtil.dateFormat(dataDate,"yyyy-MM-dd");
        log.info("本次执行应当统计的时间"+ dataDay);

        if(StringUtils.isNotBlank(maxTime)){

            if(maxTime.equals(dataDay)){
                log.info("数据已统计 任务结束");
                return;
            }
        }

        log.info(dataDay+" 数据未生成 开始统计");

        List<TerminalTargetDayCount> terminalTargetDayCounts = targetCountDao.terminalTargetDayCountTask(dataDay);

        Integer dataSize = terminalTargetDayCounts.size();
        log.info("本次共生成数据"+dataSize+"条");
        if(dataSize>0){
            targetCountDao.insert(terminalTargetDayCounts);
        }

        log.info("本次统计任务结束");

    }
}
